<?php

class Default_CategoriasController extends Zend_Controller_Action {

    public function init() {
        $this->view->categorymenu = 'id="current"';
    }

    public function indexAction() {
        $page = $this->_getParam('page');
        $page = (isset($page)) ? $page : 1;

        if (!Nosql_Util_Validator::validateInt($page)) {
            throw new Exception('Valor invalido en los parametros');
        }

        $this->view->title = ' Categorias - Pagina ' . $page;

        //get all the posts
        $categoryController = new Nosql_Model_Mysql_Category();

        $categories = $categoryController->listCategories($page);

        $this->view->categories = $categories['items'];
        if (!isset($this->view->categories) || count($this->view->categories) == 0) {
            $this->view->error = 'No se encontraron Categorias';
        } else {
            $this->view->paginator = $categories['paginator'];
            $this->view->urlPagination = '/categorias/categoria/';
        }
    }

    public function categoriaAction() {
        $categoryId = $this->_getParam('id');

        $categoryModel = new Nosql_Model_Mysql_Category();
        $postModel = new Nosql_Model_Mysql_Post();
        $category = null;
        if (is_numeric($categoryId)) {
            $category = $categoryModel->getCategoryById($categoryId);
            if ($category) {

                $page = $this->_getParam('page');
                $page = (isset($page)) ? $page : 1;

                $this->view->title = ' Categoria ' . $category['name'] . '- Pagina ' . $page;
                $this->view->category = $category;

                $posts = $postModel->searchPostsByField($categoryId, 'categoryid', $page);

                $this->view->posts = $posts['items'];
                if (!isset($this->view->posts) || count($this->view->posts) == 0) {
                    $this->view->error = 'No se encontraron artículos para esta categoría';
                } else {
                    $this->view->paginator = $posts['paginator'];
                    $this->view->urlPagination = '/categorias/categoria/id/' . $categoryId . '/';
                }
            }
        }
    }

}

